package com.aft.terra	.modules.game.model;

import java.util.Date;

import com.aft.terra.common.utils.DateUtil;
import com.aft.terra.modules.game.model.base.BaseOrders;
import com.jfinal.plugin.activerecord.Db;

public class Orders extends BaseOrders<Orders> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final Orders dao = new Orders();

	public Integer queryCount(Long userId) {
		StringBuilder sql = new StringBuilder();
		sql.append("select Ifnull(sum(game_count),0)  from t_orders where user_id = ? and pay_success = 1 ");
		return Db.queryBigDecimal(sql.toString(), userId).intValue();
	}
	
	public Integer queryCount(Long userId,String date) {
		StringBuilder sql = new StringBuilder();
		sql.append("select Ifnull(sum(game_count),0)  from t_orders where user_id = ? and pay_success = 1 ")
			.append(" and modified_date between '")
		    .append(date)
		    .append(" 00:00:00'")
		    .append(" and '")
		    .append(date)
		    .append(" 23:59:59'");
		return Db.queryBigDecimal(sql.toString(), userId).intValue();
	}
	
	public boolean setOrderPaySuccess(String orderId) {
		String sql  = "UPDATE t_orders SET pay_success = 1,modified_date = ? where  orders_code = ? ";
		return Db.update(sql, new Date(),orderId) > 0;
	}

	public Orders findByCode(String orderId) {
		String sql ="select * from t_orders where orders_code = ? ";
		return findFirst(sql,orderId);
	}
	
	public Integer findByUserId(Long userId){
		String date = DateUtil.formatDate(new Date());
		String startDate  = date +" 00:00:00";
		String endDate = date +" 23:59:59";
		StringBuffer sql = new StringBuffer();
		sql.append("select  IFNULL(sum(game_count),0) from t_orders where user_id = ? and pay_success = 1")
		.append(" and modified_date between '")
		.append(startDate).append("' and '").append(endDate ).append("'");
		return Db.queryBigDecimal(sql.toString(), userId).intValue();
	}

}
